package com.peanut.wapp.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.peanut.wapp.controller.res.PageResult;
import com.peanut.wapp.controller.resBean.RNewsBean;
import com.peanut.wapp.model.NewsBean;
import com.peanut.wapp.persist.NewsDao;
import com.peanut.wapp.service.NewsService;
import com.peanut.wapp.util.DateTimeUtil;

@Service
public class NewsServiceImpl implements NewsService {
				
	@Autowired
	NewsDao newsImpl;

	@Override
	public PageResult<RNewsBean> getList(int page, int pageSize) {
		PageResult<RNewsBean> result = new PageResult<RNewsBean>();
		int count = newsImpl.selectCount();
		List<NewsBean> resList = newsImpl.selectList(page, pageSize);
		List<RNewsBean> resData = new ArrayList<>();
		for(NewsBean co : resList) {
			RNewsBean rc = new RNewsBean();
			rc.setNid(co.getNid());
			rc.setTitle(co.getTitle());
			rc.setAuthor(co.getAuthor());
			rc.setCreateTime(co.getCreateTime()!= null?co.getCreateTime().getTime()/1000:null);
			rc.setSubTitle(co.getSubTitle());
			resData.add(rc);
		}
		result.setCount(count);
		result.setData(resData);
		return result;
	}

	@Override
	public RNewsBean getDetail(String nid) {
		RNewsBean res = new RNewsBean();
		NewsBean news = newsImpl.selectOneById(nid);
		res.setNid(nid);
		res.setAuthor(news.getAuthor());
		res.setContent(news.getContent());
		res.setTitle(news.getTitle());
		res.setCreateTime(news.getCreateTime()!= null?news.getCreateTime().getTime():null);
		res.setCreateTimeStr(news.getCreateTime()!= null?DateTimeUtil.formatTimestamp(news.getCreateTime().getTime()/1000, null):null);
		return res;
	}


}
